import axios from 'axios'
import { Loading } from 'element-ui';

let loadingInstance  = null;
// 添加请求拦截器
axios.interceptors.request.use( (config)=>{
    // 在发送请求之前做些什么
	
	loadingInstance = Loading.service({ fullscreen: true,background:'rgba(0,0,0,0.7)', text: '加载中请稍等', });
	// console.log("计划打开loading....")
	return config;
// 对请求错误做些什么
	return Promise.reject(error);
});


// 添加响应拦截器
axios.interceptors.response.use( (response)=> {
 
	loadingInstance.close()
	// console.log("关闭loading....")
    return response;
  }, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
  });




const http = {
	get(url, callback, params = {}) {

		axios.get(url, {
			params: params
		}).then((response) => {

			callback(response.data)
		}).catch(function(error) {
			// 处理错误情况
			console.log(error);
		});
	},
	
	post(url, callback, params = {}){
		
		const urlParams = new URLSearchParams();
		console.log(params);
		for(let key in params){
			console.log("key",key)
			urlParams.append(key, params[key]);
		}
		console.log(urlParams)
		
		axios.post(url, urlParams)
		.then((ret)=> {
			
			let data = ret.data;
			callback(data)
		})
		.catch(function(error) {
			console.log(error);
		});
		
	}
}

export default http
